package com.jm.offer;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description
 * @date 2022/4/13 9:01
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ListNode {
    int val;
    ListNode next;
    public ListNode() {}

    public ListNode(int x) { val = x; }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }



    /**
     * 创建一个升序链表
     * @return
     */
    public static ListNode createLinkedListByASC(){
        return new ListNode(1,new ListNode(2, new ListNode(3,new ListNode(4))));
    }

    /**
     * 创建一个降序链表
     * @return
     */
    public static ListNode createLinkedListByDESC(){
        return new ListNode(4,new ListNode(3, new ListNode(2,new ListNode(1))));
    }

    /**
     * 创建一个随机链表
     * @return
     */
    public static ListNode createLinkedListByRandom(){
        return new ListNode((int) (Math.random() * 100),new ListNode((int) (Math.random() * 100), new ListNode((int) (Math.random() * 100),new ListNode((int) (Math.random() * 100)))));
    }

    /**
     * 创建一个指定长度的随机链表
     * @return
     */
    public static ListNode createLinkedListByRandom(int size){
        if (size == 0){
            return null;
        }
        ListNode head = new ListNode((int) (Math.random() * 100));
        ListNode node = head;
        for (int i = 0; i < size - 1; i++) {
            node.next = new ListNode((int) (Math.random() * 100));
            node = node.next;
        }
        return head;
    }


    /**
     * 传入头节点打印链表
     * @param head
     */
    public static void printLinkedList(ListNode head){
        ListNode node = head;
        while (node!=null){
            System.out.println(node.val);
            node = node.next;
        }
    }
}
